package com.wwh.mapper;

import org.apache.ibatis.annotations.*;
import com.wwh.model.Post;

import java.util.List;

@Mapper
public interface PostMapper {
    @Select("select * from post_ ")
    List<Post> findAll();

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert(" insert into post_ ( title, content, created_at, updated_at ) values (#{title}, #{content}, unix_timestamp(now()), unix_timestamp(now())) ")
    public int save(Post post);

    @Delete(" delete from post_ where id= #{id} ")
    public void delete(int id);

    @Select("select * from post_ where id= #{id} ")
    public Post get(int id);

    @Update("update post_ set title=#{title},content=#{content},updated_at=unix_timestamp(now()) where id=#{id} ")
    public int update(Post post);

    @Select("select * from post_ where ")
    public Post getByName(String name);

    @Select("select * from post_ where id in (select post_id from relation_ where user_id=#{userId})")
    List<Post> findSelfPost(int userId);
}
